import requests
import re
import csv
import time

url = f"https://tieba.baidu.com/hottopic/browse/topicList?res_type=1"
response = requests.get(url)
# print(response)
# print(response.text)
r = response.text
data = re.findall(r'<a href=".*?" target="_blank" class="topic-text">(.*?)</a><span class="topic-num">(.*?)</span></div><p class="topic-top-item-desc">(.*?)</p></div></li>',r)
# print(data[0],type(data[0]))

with open("贴吧热议.csv","w",encoding="utf8",newline='') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(['标题', '实时讨论', '内容'])
    for i in range(len(data)):
        print(f"正在写入{data[i][0]}")

        csvwriter.writerow([data[i][0],data[i][1],data[i][2]])
        time.sleep(1)
